<template>
    <view class="box major-introduce">
        <view class="title">{{ title }}</view>
        <view class="content" :class="{ 'content-expand': expand }" ref="contentRef" v-html="content">
        </view>
        <view class="expand" ref="expandRef" v-show="showExpand" @tap="clickExpand">
            展开全部
            <image class="image" src="/static/icon/expand-icon.png" />
        </view>
    </view>
</template>
  
<script>
export default {
    props: {
        title: {
            type: String,
            default: '',
        },
        content: {
            type: String,
            default: 'aa<br />bb',
        },
    },
    data() {
        return {
            showExpand: false,
            expand: true,
        };
    },
    mounted() {
        this.checkContentLines();
    },
    methods: {
        checkContentLines() {
            const query = uni.createSelectorQuery().in(this);
            query.select('.content').boundingClientRect(rect => {
                const contentHeight = rect.height;
                const lineHeight = 30;
                const lines = Math.ceil(contentHeight / lineHeight);
                this.showExpand = lines >= 5;
            }).exec();
        },
        clickExpand() {
            this.showExpand = false;
            this.expand = false;
        }
    }
};
</script>
  
<style lang="less" scoped>
.box {
    padding: 30upx;
    background-color: white;
    margin-top: 20upx;
    line-height: 60upx;

    .title {
        font-size: 30upx;
        font-weight: 700;
        padding: 10upx 0;
    }

    .content {
        font-size: 26upx;
    }

    .content-expand {
        display: -webkit-box;
        -webkit-line-clamp: 5;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .expand {
        width: 100%;
        text-align: center;
        color: #d01117;
        font-size: 28upx;

        .image {
            width: 26upx;
            height: 22upx;
            padding: 0 5upx;
        }
    }
}
</style>